<template>
  <div class="add-movie-container">
    <h1>添加电影</h1>
    <form @submit.prevent="handleSubmit" class="movie-form">
      <div class="form-group">
        <label for="title">电影标题</label>
        <input 
          id="title" 
          v-model="form.title" 
          type="text" 
          placeholder="请输入电影标题"
          required
        >
      </div>
      
      <div class="form-group">
        <label for="year">上映年份</label>
        <input 
          id="year" 
          v-model="form.year" 
          type="number" 
          placeholder="请输入上映年份"
          required
        >
      </div>
      
      <div class="form-group">
        <label for="director">导演</label>
        <input 
          id="director" 
          v-model="form.director" 
          type="text" 
          placeholder="请输入导演"
          required
        >
      </div>
      
      <div class="form-group">
        <label for="actors">主演</label>
        <input 
          id="actors" 
          v-model="form.actors" 
          type="text" 
          placeholder="请输入主演，用逗号分隔"
          required
        >
      </div>
      
      <div class="form-group">
        <label for="genre">类型</label>
        <input 
          id="genre" 
          v-model="form.genre" 
          type="text" 
          placeholder="请输入类型，如：剧情,动作"
          required
        >
      </div>
      
      <div class="form-group">
        <label for="runtime">时长(分钟)</label>
        <input 
          id="runtime" 
          v-model="form.runtime" 
          type="number" 
          placeholder="请输入电影时长"
          required
        >
      </div>
      
      <div class="form-group">
        <label for="rating">评分</label>
        <input 
          id="rating" 
          v-model="form.rating" 
          type="number" 
          step="0.1" 
          min="0" 
          max="10"
          placeholder="请输入评分(0-10)"
        >
      </div>
      
      <div class="form-group">
        <label for="poster">海报URL</label>
        <input 
          id="poster" 
          v-model="form.poster" 
          type="url" 
          placeholder="请输入海报图片URL"
        >
      </div>
      
      <div class="form-group">
        <label for="plot">剧情简介</label>
        <textarea 
          id="plot" 
          v-model="form.plot" 
          rows="5" 
          placeholder="请输入剧情简介"
          required
        ></textarea>
      </div>
      
      <button type="submit" class="submit-btn" :disabled="submitting">
        {{ submitting ? '提交中...' : '提交' }}
      </button>
    </form>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '../stores/auth'
import api from '../composables/useApi'

const form = ref({
  title: '',
  year: '',
  director: '',
  actors: '',
  genre: '',
  runtime: '',
  rating: '',
  poster: '',
  plot: ''
})

const submitting = ref(false)
const router = useRouter()
const authStore = useAuthStore()

const handleSubmit = async () => {
  submitting.value = true
  try {
    const movieData = {
      ...form.value,
      userId: authStore.user.id,
      username: authStore.user.username
    }
    
    await api.addMovie(movieData)
    alert('电影添加成功')
    router.push('/movies')
  } catch (error) {
    console.error('添加电影失败:', error)
    alert('添加电影失败')
  } finally {
    submitting.value = false
  }
}
</script>

<style scoped>
.add-movie-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 2rem;
}

h1 {
  text-align: center;
  margin-bottom: 2rem;
  color: #333;
}

.movie-form {
  background: #fff;
  padding: 2rem;
  border-radius: 8px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

.form-group {
  margin-bottom: 1.5rem;
}

label {
  display: block;
  margin-bottom: 0.5rem;
  font-weight: bold;
}

input, textarea {
  width: 100%;
  padding: 0.75rem;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 1rem;
}

textarea {
  resize: vertical;
}

.submit-btn {
  width: 100%;
  padding: 0.75rem;
  background-color: #42b983;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 1rem;
  cursor: pointer;
}

.submit-btn:disabled {
  background-color: #ccc;
  cursor: not-allowed;
}

@media (max-width: 768px) {
  .add-movie-container {
    padding: 1rem;
  }
  
  .movie-form {
    padding: 1rem;
  }
}
</style>